function logout() {
    if (!confirm("确认退出")) {
        return 0;
    }
    $.ajax({
        url: "admin-posts.php",
        type: "POST",
        async: true,
        timeOut: 50000,
        dataType: "json",
        data: {
            type: "logout"
        },
        success: function (data, textStatus, jqXHR) {
            if (data.content === 0) {
                window.location.href = "./login.php";
            } else
                alert(data.error);
        },
        error: function (xhr, textStatus) {
            console.log(xhr + textStatus);
        }
    })
}

function look_form(uid) {
    var poster_id = $("#select-poster-id option:selected").val();
    if (poster_id != 'all') {
        window.location.href = "../form.php?uid=" + uid + "&fid=" + poster_id;
    }
}

function addNewForm(uid, fid) {
    xcsoft.loading("加载中...");
    $.ajax({
        url: "admin-posts.php",
        type: "POST",
        async: true,
        timeOut: 50000,
        dataType: "json",
        data: {
            type: "addNewForm",
            uid: uid,
            fid: fid
        },
        success: function (data, textStatus, jqXHR) {
            xcsoft.success("加载成功", 1000);
            if (data.content === 0) {
                window.location.href = "../index.php";
            } else
                alert(data.error);
        },
        error: function (xhr, textStatus) {
            xcsoft.error("加载失败", 1000);
            console.log(xhr + textStatus);
        }
    })
}

function look_poster(uid, im) {
    if (im === '0') {
        return 0;
    } else {
        window.location.href = "../share.php?uid=" + uid + "&pn=" + im;
    }
}

function updateUserSets(num) {
    switch (num) {
        case 0:

            break;
        case 1:

            break;
        case 2:
            var post = true;
            var $listName = $(".list-name");
            var listNameArray = [];
            $listName.each(function () {
                var listName = $(this).html();
                listNameArray.push(listName);
            })
            var $listInput = $("input");
            var listInputArray = [];
            $listInput.each(function () {
                var listInput = $(this).val();
                if (post === true) {
                    if (listInput === '') {
                        $(this).focus();
                        xcsoft.info('请填写你的相关信息哟', 1000);
                        post = false;
                    } else {
                        listInputArray.push(listInput);
                    }
                }
            })
            var listJson = JSON.stringify([listNameArray, listInputArray]);
            console.log(listJson);
            if (post === false) return 0;
            $.ajax({
                url: "admin-posts.php",
                type: "POST",
                async: true,
                timeOut: 50000,
                dataType: "json",
                data: {
                    type: "addUserSets",
                    mode: 2,
                    listJson: listJson
                },
                success: function (data, textStatus, jqXHR) {
                    if (data.state === 0) {
                        xcsoft.success('你的信息更新成功', 1000);
                    } else {
                        console.log(data.state);
                    }
                },
                error: function (xhr, textStatus) {
                    console.log(xhr + textStatus);
                }
            })
            break;
        default:
    }
}


function adminSharePoster(uid, im) {
    $.ajax({
        url: "admin-posts.php",
        type: "POST",
        async: true,
        timeOut: 50000,
        dataType: "json",
        data: {
            type: "adminSharePoster",
            uid: uid
        },
        success: function (data, textStatus, jqXHR) {
            if (data.state === 0) {
                window.location.href = "../share.php?uid=" + uid + "&pn=" + im;
            }
        },
        error: function (xhr, textStatus) {
            console.log(xhr + textStatus);
        }
    })
}

function deletePoster(uid, pid, type) {
    if (!confirm("删除海报将同时删除表格，确认删除？")) return 0;
    $.ajax({
        url: "admin-posts.php",
        type: "POST",
        async: true,
        timeOut: 50000,
        dataType: "json",
        data: {
            type: "deletePoster",
            uid: uid,
            pid: pid
        },
        success: function (data, textStatus, jqXHR) {
            console.log(data);
            if (data.state === 0) {
                if (type === 1) {
                    window.location.href = "./myPoster.php?delete=1";
                } else {
                    xcsoft.success("删除成功", 1000);
                    $(".active").each(function () {
                        var ul = $(this).parent(".list1")
                        $(this).remove();
                        if (ul.children('li').length == 0) {
                            console.log(312)
                            ul.parent("li").remove()
                        }
                    })
                }
            } else alert(data.error);
        },
        error: function (xhr, textStatus) {
            console.log(xhr + textStatus);
        }
    })
}

function editPoster(uid, pid) {
    $.ajax({
        url: "../posts.php",
        type: "POST",
        async: true,
        timeOut: 50000,
        dataType: "json",
        data: {
            type: "editPoster",
            uid: uid,
            pid: pid
        },
        success: function (data, textStatus, jqXHR) {
            console.log(data);
            if (data.state === 0) {
                window.location.href = "../make.php";
            } else alert(data.error);
        },
        error: function (xhr, textStatus) {
            console.log(xhr + textStatus);
        }
    })
}

function deleteApply2(uid, tid) {
    if (!confirm("删除后数据不会还原，确认删除？")) return 0;
    var idArray = [];
    $(".my-apply2 .list .check").each(function () {
        if ($(this).hasClass('check2')) {
            idArray.push($(this).children("id").html());
        }
    })
    console.log(idArray);
    var idJson = JSON.stringify(idArray);
    $.ajax({
        url: "admin-posts.php",
        type: "POST",
        async: true,
        timeOut: 50000,
        dataType: "json",
        data: {
            type: "deleteApply2",
            uid: uid,
            tid: tid,
            idJson: idJson
        },
        success: function (data, textStatus, jqXHR) {
            console.log(data);
            if (data.state === 0) {
                $(".my-apply2 .list .check").each(function () {
                    if ($(this).hasClass('check2')) {
                        $(this).remove();
                    }
                })
                xcsoft.success('删除成功', 1000);
                //刪完后 全选按鈕变色
                if ($(".my-apply2 .list>li").length === 0) {
                    $(".my-apply2 .check-all").removeClass('check2').addClass('check1')
                }
            } else alert(data.error);
        },
        error: function (xhr, textStatus) {
            console.log(xhr + textStatus);
        }
    })
}

function deleteApply3(uid, tid, pid) {
    if (!confirm("确认删除？")) return 0;
    $.ajax({
        url: "admin-posts.php",
        type: "POST",
        async: true,
        timeOut: 50000,
        dataType: "json",
        data: {
            type: "deleteApply3",
            uid: uid,
            tid: tid,
            pid: pid
        },
        success: function (data, textStatus, jqXHR) {
            console.log(data);
            if (data.state === 0) {
                window.location.href = "./myApply2.php?tid=" + tid + "&delete=1";
            } else alert(data.error);
        },
        error: function (xhr, textStatus) {
            console.log(xhr + textStatus);
        }
    })
}